package conson.tech.manage.app.controller.mobile;

import cn.hutool.json.JSONUtil;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.api.app.constant.AppCommon;
import conson.tech.manage.api.app.dto.AppConfigDTO;
import conson.tech.manage.api.app.dto.AppGroupConfigDTO;
import conson.tech.manage.api.app.dto.AppPageConfigDTO;
import conson.tech.manage.api.app.form.AppQueryForm;
import conson.tech.manage.app.entity.pojo.AppConfig;
import conson.tech.manage.app.entity.pojo.AppPageConfig;
import conson.tech.manage.app.entity.pojo.AppVersion;
import conson.tech.manage.app.service.AppConfigService;
import conson.tech.manage.app.service.AppGroupConfigService;
import conson.tech.manage.app.service.AppPageConfigService;
import conson.tech.manage.app.service.AppVersionService;
import conson.tech.manage.common.util.EntityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AppMobileController
 * Description:
 * Author: HXL
 * Date: 2020/5/9 4:56 下午
 * Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping(SecurityConstants.MOBILEAPI)
public class AppMobileController {

    @Autowired
    private AppPageConfigService appPageConfigService;
    @Autowired
    private AppGroupConfigService appGroupConfigService;
    @Autowired
    private AppConfigService appConfigService;
    @Autowired
    private AppVersionService appVersionService;

    @PostMapping("/queryDefault")
    public Response<List<AppConfigDTO>> queryDefaultApp( @RequestBody AppQueryForm form){
        log.info("查询首页默认应用入参为:[{}]", JSONUtil.toJsonStr(form));
        Integer versionValue = getVersionValue(form);//获取版本值
        if (versionValue == null){
            return Response.success(new ArrayList<>());
        }
        AppConfig appConfig = new AppConfig();
        appConfig.setDefaultIndex(AppCommon.APP_DEFAULT_ONE);
        List<AppConfigDTO>  configDTOList = appConfigService.queryAppConfigList(appConfig,versionValue);
        return Response.success(configDTOList);
    }
    /**
     * Title: 查询-首页生活页
     * Description:
     * @param form
     * @return: conson.tech.core.response.Response<java.util.List<conson.tech.api.app.dto.AppPageConfigDTO>>
     * Company: GX
     * Author HXL
     * Date 2020/5/18 9:58 上午
     */
    @PostMapping("/queryPage")
    public Response<List<AppPageConfigDTO>> appQueryPageInfo(@RequestBody AppQueryForm form){
        log.info("查询首页生活页入参为:[{}]", JSONUtil.toJsonStr(form));
        if (form == null || form.getType() == null){
            return Response.success(new AppPageConfigDTO());
        }
        Integer versionValue = getVersionValue(form);//获取版本值
        if (versionValue == null){
           return Response.success(new AppPageConfigDTO());
        }
        List<AppPageConfig>  list = appPageConfigService.queryAppPageConfigByPageType(form.getType(),versionValue);
       if (CollectionUtils.isEmpty(list)){
           return Response.success(new AppPageConfigDTO());
       }
        List<AppPageConfigDTO> dtoList = EntityUtils.listToList(list, AppPageConfigDTO.class);
        return Response.success(dtoList);
    }

    private Integer getVersionValue(AppQueryForm form) {
        List<AppVersion> versionList = appVersionService.queryAppVersionByVersionNum(form.getVersion(),form.getPlatformType());
        if (CollectionUtils.isEmpty(versionList)){
            return  null;
        }
        if (versionList.get(0).getVersionState() == AppCommon.VERSION_STATE_ONE){//是最新版本
            return AppCommon.VERSION_VALUE;
        }else {
            return versionList.get(0).getVersionValue();
        }
    }

    /**
     * Title:
     * Description: 查询分组应用-专题、功能应用
     * @param form
     * @return: conson.tech.core.response.Response<conson.tech.api.app.dto.AppGroupConfigDTO>
     * Company: GX
     * Author HXL
     * Date 2020/5/18 10:30 上午
     */
    @PostMapping("/queryGroup")
    public Response<List<AppGroupConfigDTO>> appQueryGroup( @RequestBody AppQueryForm form){
        log.info("查询分组应用-专题、功能应用入参为:[{}]", JSONUtil.toJsonStr(form));
        if (form == null || form.getType() == null){
            return Response.success(new ArrayList<>());
        }
        Integer versionValue = getVersionValue(form);//获取版本值
        if (versionValue == null){
            return Response.success(new ArrayList<>());
        }
        List<AppGroupConfigDTO>  list = appGroupConfigService.queryAppGroupByType(form.getType(),versionValue);
        return Response.success(list);
    }
    /**
     * Title: 查询信豆页
     * Description:
     * @param form
     * @return: conson.tech.core.response.Response
     * Company: GX
     * Author HXL
     * Date 2020/5/19 3:37 下午
     */
    @PostMapping("/queryLetterBean")
    public Response<List<AppConfigDTO>> appQueryLetterBean(@RequestBody AppQueryForm form){
        log.info("查询信豆页入参为:[{}]", JSONUtil.toJsonStr(form));
        Integer versionValue = getVersionValue(form);//获取版本值
        if (versionValue == null){
            return Response.success(new ArrayList<>());
        }
        AppConfig appConfig = new AppConfig();
        appConfig.setPlateType(AppCommon.APP_PLATE_TYPE_FOUR);
        List<AppConfigDTO> list = appConfigService.queryAppConfigList(appConfig,versionValue);
        return Response.success(list);
    }
}
